﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

using MaxStudio.Concrete.DBUtility;

namespace MaxStudio.Concrete.Server.Dao
{
    public partial class ConcreteDaoDataContext : System.Data.Linq.DataContext
    {
        #region user 
        public List<tb_User> GetAllUserList(int currentPage, int PageSize )
        {
           
            var query = (from obj in this.tb_Users
                         where obj.IsDelete == 0
                         select obj).ToList();
            return query;
        }

        public tb_User GetUserById(int id)
        {
            return this.tb_Users.SingleOrDefault(tb_Users => tb_Users.Id==id); 
        }

        public void CreateUser(tb_User instance)
        {
            instance.CreateDate = DateTime.Now;
            instance.CreateBy = 1;
            instance.ModifyDate = DateTime.Now;
            instance.ModifyBy = 1;
            tb_Users.InsertOnSubmit(instance);
            this.SubmitChanges();
        }

        public void ModifyUser(int id, int isDelete, string name, string pwd, string tel, int userGroup, tb_FunctionGroup tb_FunctionGroups)
        {
            tb_User instance = this.tb_Users.SingleOrDefault(p => p.Id == id);

            instance.IsDelete=isDelete;
            instance.ModifyBy=1; //Need to get current user
            instance.ModifyDate=DateTime.Now;
            instance.Name=name;
            instance.PWD=pwd;
            instance.Tel = tel;
            instance.UserGroup = userGroup;
            instance.tb_FunctionGroup = tb_FunctionGroups;

            this.SubmitChanges();
        }
        public void DeleteUser(int id)
        {
            tb_User entity = tb_Users.SingleOrDefault(p => p.Id == id);
            tb_Users.DeleteOnSubmit(entity);
            this.SubmitChanges(); 
        }

        public List<tb_User> GetUserByGroup(int groupId)
        {
            var query = (from obj in this.tb_Users
                         where obj.tb_FunctionGroup.Id == groupId
                         select obj).ToList();
            return query;
        }
        #endregion
    }
}
